package com.blog.cloud.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.cloud.api.dto.UserInfoDto;
import com.blog.cloud.api.dto.UserRegisterDto;
import com.blog.cloud.api.entity.User;
import com.blog.cloud.core.utils.R;
import com.blog.cloud.core.valid.AddGroup;
import com.blog.cloud.service.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户表(User)表控制层
 *
 * @author 龙茶清欢
 * @since 2023-02-26 18:48:26
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Tag(name = "用户表管理", description = "用户表管理")
public class UserController {
  
    private final UserService userService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param user 查询实体
     * @return 所有数据
     */
    @GetMapping("/findUserByPage")
    @Operation(description = "通过分页查询用户")
    public R<Object> findUserByPage(Page<User> page, User user) {
        return R.ok(userService.page(page, new QueryWrapper<>(user)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/findUserByUserId/{userId}")
    @Operation(description = "通过ID查询")
    public R<Object> findUserByUserId(@PathVariable Long userId) {
        return R.ok(userService.getById(userId));
    }

    /**
     * 新增数据
     *
     * @param user 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @Operation(description = "新增")
    @PreAuthorize("hasAuthority('sys:user:insert')")
    public R<Object> insert(@RequestBody @Validated(AddGroup.class) User user) {
        return R.ok(userService.save(user));
    }

    /**
     * 修改数据
     *
     * @param user 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateUser")
    @Operation(description = "更新用户信息")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public R<Object> update(@RequestBody User user) {
        // 如果密码不为空，则进行加密处理
        return R.ok(userService.updateById(user));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteUser")
    @Operation(description = "删除")
    @PreAuthorize("hasRole('admin')")
    public R<Object> delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(userService.removeByIds(idList));
    }

    @PostMapping("/findUserByUsername")
    @Operation(description = "根据用户名查询用户")
    R<UserInfoDto> findUserByUsername(@RequestBody String username) {
        UserInfoDto userInfoDto = userService.findUserByUsername(username);
        return R.ok(userInfoDto);
    }

    @PostMapping("/findUserByMobile")
    @Operation(description = "根据手机号查询用户")
    R<UserInfoDto> findUserByMobile(@RequestBody String mobile) {
        UserInfoDto userInfoDto = userService.findUserByMobile(mobile);
        return R.ok(userInfoDto);
    }

    @PostMapping("/user/findUserByFlag")
    @Operation(description = "根据用户唯一标识查询用户")
    R<User> findUserByFlag(@RequestBody @Valid UserRegisterDto registerDto) {
        User user = userService.findUserByFlag(registerDto);
        return R.ok(user);
    }

    @PostMapping("/user/saveUser")
    @Operation(description = "用户注册")
    R<Boolean> saveUser(@RequestBody @Valid UserRegisterDto registerDto) {
        Boolean result = userService.saveUser(registerDto);
        return R.ok(result);
    }
    
}

